<template>
  <!--  <page-layout :title="description">-->
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="8" :lg="7" :md="8" :sm="24">
            <a-form-item label="客户业务编号">
              <a-input placeholder="请输入编号" v-model="queryParam.customerNumber"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="7" :md="8" :sm="24">
            <a-form-item label="订单编号">
              <a-input placeholder="请输入编号" v-model="queryParam.orderNumber"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="8" :lg="7" :md="8" :sm="24">
            <a-form-item label="箱号">
              <a-input placeholder="请输入箱号" v-model="queryParam.boxInfo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="7" :md="8" :sm="24">
            <a-form-item label="业务主单号">
              <a-input placeholder="请输入主单号" v-model="queryParam.mainOffNumber"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="7" :md="8" :sm="24">
            <a-form-item label="分单号">
              <a-input placeholder="请输入分单号" v-model="queryParam.otherOffNumber"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="7" :md="8" :sm="24">
            <a-form-item label="起运港">
              <a-auto-complete allowClear v-model="queryParam.polDesc" placeholder="起运港"
                               :dataSource="portData" @search="portHandleSearch" />
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="7" :md="8" :sm="24">
            <a-form-item label="卸货港">
              <a-auto-complete allowClear v-model="queryParam.podRemark" placeholder="卸货港"
                               :dataSource="portData" @search="portHandleSearch" />
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="7" :md="8" :sm="24">
            <a-form-item label="交货地">
              <a-auto-complete allowClear v-model="queryParam.placeDeliveryDesc" placeholder="交货地"
                               :dataSource="portData" @search="portHandleSearch" />
            </a-form-item>
          </a-col>
          <!--          <template v-if="toggleSearchStatus">-->
          <!--          </template>-->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <!--              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>-->
              <j-super-query :fieldList="fieldList" ref="superQueryModal"
                             @handleSuperQuery="handleSuperQuery"
                             style="margin-left: 8px"></j-super-query>
              <!--              <a @click="handleToggleSearch" style="margin-left: 8px">-->
              <!--                {{ toggleSearchStatus ? '收起' : '展开' }}-->
              <!--                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />-->
              <!--              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="handleExportXls('海运数据')" type="primary" ghost icon="download">导出</a-button>
      <!--      <router-link target="_blank" :to="{path:'/report/Demo'}">新页面打开home页</router-link>-->
      <!--      <a-button type="primary" icon="download" @click="handleExportXls('海运出口')">导出</a-button>-->
      <!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader"  @change="handleImportExcel">-->
      <!--        <a-button type="primary" icon="import">导入</a-button>-->
      <!--      </a-upload>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- table区域-begin -->
      <div class="ant-alert ant-alert-info" style="margin-bottom: 8px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <a-popover title="自定义列" trigger="click" placement="leftBottom" style="float: right">
          <template slot="content">
            <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
              <a-row>
                <template v-for="(item,index) in defColumns">
                  <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                    <a-col :span="12">
                      <a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox>
                    </a-col>
                  </template>
                </template>
              </a-row>
            </a-checkbox-group>
          </template>
          <a>
            <a-icon type="setting" />
            自定义列</a>
        </a-popover>

        <a style="margin-right:20px;float: right" @click="showHiddenActionCol">
          {{ actionLabel }}
        </a>
        <span style="margin-right:20px;float: right">
          <a-select size="small" defaultValue="5000" style="width:120px" @change="refreshData">
           <a-select-option value="5000">每5秒刷新</a-select-option>
           <a-select-option value="30000">每30秒刷新</a-select-option>
           <a-select-option value="60000">每1分钟刷新</a-select-option>
          </a-select>
        </span>
      </div>
      <a-table
        ref="table"
        size="small"
        :scroll="{x:1500,y:tableHeight}"
        :style="{'min-height':tableHeight+'px'}"
        bordered
        rowKey="id"
        :columns="columns"
        :components="components"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
        :customRow="handleSelectEdit">

        <!--        <template slot="htmlSlot" slot-scope="text">-->
        <!--          <div v-html="text"></div>-->
        <!--        </template>-->
        <!--        <template slot="imgSlot" slot-scope="text">-->
        <!--          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>-->
        <!--          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>-->
        <!--        </template>-->
        <!--        <template slot="fileSlot" slot-scope="text">-->
        <!--          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>-->
        <!--          <a-button-->
        <!--            v-else-->
        <!--            :ghost="true"-->
        <!--            type="primary"-->
        <!--            icon="download"-->
        <!--            size="small"-->
        <!--            @click="downloadFile(text)">-->
        <!--            下载-->
        <!--          </a-button>-->
        <!--        </template>-->
        <template #footer>{{ Footer }}</template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <!--          <a-divider type="vertical" />-->
          <!--          <a-dropdown>-->
          <!--            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>-->
          <!--            <a-menu slot="overlay">-->
          <!--              <a-menu-item>-->
          <!--                <a @click="handleDetail(record)">详情</a>-->
          <!--              </a-menu-item>-->
          <!--              <a-menu-item>-->
          <!--                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">-->
          <!--                  <a>删除</a>-->
          <!--                </a-popconfirm>-->
          <!--              </a-menu-item>-->
          <!--            </a-menu>-->
          <!--          </a-dropdown>-->
        </span>

      </a-table>
    </div>

    <ship-export-modal v-if='addModalVisible' ref="modalForm" @close="close" @ok="modalFormOk"></ship-export-modal>
  </a-card>
  <!--  </page-layout>-->
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ShipExportModal from './modules/ShipExportModal'
import JDate from '@/components/jeecg/JDate.vue'
import PageLayout from '@/components/page/PageLayout'
import Vue from 'vue'

import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
import { getAction } from '@api/manage'

const superQueryFieldList = [
  {
    type: 'string',
    value: 'customerNumber',
    text: '客户业务编号'
  }, {
    type: 'select',
    value: 'mainOffNumber',
    text: '主单号'
  }, {
    type: 'select',
    value: 'otherOffNumber',
    text: '分单号'
  }, {
    type: 'string',
    value: 'boxInfo',
    text: '箱号'
  }, {
    type: 'string',
    value: 'orderNumber',
    text: '订单编号'
  }, {
    type: 'date',
    value: 'etd',
    text: 'ETD'
  }, {
    type: 'select',
    value: 'shipmentMode',
    text: '装运方式',
    dictCode: 'shipmeng_mode'
  }, {
    type: 'select',
    value: 'entrustCompany',
    text: '委托单位',
    dictCode: '',
    options: []
  }, {
    type: 'select',
    value: 'bookingAgent',
    text: '订舱代理',
    dictCode: '',
    options: []
  }, {
    type: 'select',
    value: 'shipCompany',
    text: '船公司',
    dictCode: 'ship_company'
  }, {
    type: 'select',
    value: 'busPersonnel',
    text: '业务人员',
    dictCode: 'business_people'
  }, {
    type: 'select',
    value: 'operator',
    text: '操作人员',
    dictCode: 'operator'
  }, {
    type: 'select',
    value: 'cusServiceStaff',
    text: '客服人员',
    dictCode: 'customer_service_staff'
  }
]
export default {
  name: 'ShipExportList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDate,
    ShipExportModal,
    JSuperQuery,
    PageLayout
  },
  data() {
    this.components = {
      header: {
        cell: (h, props, children) => {
          const { key, ...restProps } = props
          const col = this.columns.find(col => {
            const k = col.dataIndex || col.key
            return k === key
          })

          if (!col || !col.width) {
            return h('th', { ...restProps }, [...children])
          }

          const dragProps = {
            key: col.dataIndex || col.key,
            class: 'table-draggable-handle',
            attrs: {
              w: 10,
              x: col.width,
              z: 1,
              axis: 'x',
              draggable: true,
              resizable: false
            },
            on: {
              dragging: (x, y) => {
                col.width = Math.max(x, 1)
              }
            }
          }
          const drag = h('vue-draggable-resizable', { ...dragProps })
          return h('th', { ...restProps, class: 'resize-table-th' }, [...children, drag])
        }
      }
    }
    return {
      actionLabel: '隐藏操作列',
      addModalVisible: false,
      description: '海运出口管理页面',
      actionColumn: {
        title: '操作',
        dataIndex: 'action',
        align: 'center',
        fixed: 'right',
        width: 147,
        scopedSlots: { customRender: 'action' }
      },
      //列设置
      settingColumns: [],
      columns: [],
      portData: [],
      // 表头
      defColumns: [
        {
          title: '#',
          dataIndex: 'rowIndex',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          },
          fixed: 'left'
        },
        {
          title: '订单编号',
          align: 'left',
          width: 160,
          dataIndex: 'orderNumber',
          ellipsis: true,
          fixed: 'left'
        },
        {
          title: '客户业务编号',
          align: 'left',
          width: 160,
          dataIndex: 'customerNumber',
          ellipsis: true,
          fixed: 'left'
        },
        {
          title: '主单号',
          align: 'center',
          width: 160,
          dataIndex: 'mainOffNumber',
          ellipsis: true
        },
        {
          title: '分单号',
          align: 'center',
          width: 160,
          dataIndex: 'otherOffNumber',
          ellipsis: true
        }, {
          title: '起运港',
          align: 'center',
          width: 200,
          dataIndex: 'polRemark',
          ellipsis: true
        },
        {
          title: '卸货港',
          align: 'center',
          width: 200,
          dataIndex: 'podRemark',
          ellipsis: true
        }, {
          title: '交货地',
          align: 'center',
          width: 200,
          dataIndex: 'placeDeliveryDesc',
          ellipsis: true
        }, {
          title: 'ETD',
          align: 'center',
          width: 200,
          dataIndex: 'etd',
          ellipsis: true
        },
        {
          title: '装运方式',
          align: 'center',
          width: 200,
          dataIndex: 'shipmentMode',
          ellipsis: true
        },
        {
          title: '箱型箱量',
          align: 'center',
          width: 200,
          dataIndex: 'boxSize',
          ellipsis: true
        },
        {
          title: '委托单位',
          align: 'center',
          width: 160,
          dataIndex: 'entrustCompany',
          ellipsis: true
        },
        {
          title: '订舱代理',
          align: 'center',
          width: 160,
          dataIndex: 'bookingAgent',
          ellipsis: true
        },
        {
          title: '船公司',
          align: 'center',
          width: 160,
          dataIndex: 'shipCompany',
          ellipsis: true
        },
        {
          title: '操作人员',
          align: 'center',
          width: 160,
          dataIndex: 'operator',
          ellipsis: true
        },
        {
          title: '业务人员',
          align: 'center',
          width: 160,
          dataIndex: 'busPersonnel',
          ellipsis: true
        },
        {
          title: '客服人员',
          align: 'center',
          width: 160,
          dataIndex: 'cusServiceStaff',
          ellipsis: true
        },

        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 100,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/box.ship/shipInfo/list',
        delete: '/box.ship/shipInfo/delete',
        deleteBatch: '/box.ship/shipInfo/deleteBatch',
        exportXlsUrl: '/box.ship/shipInfo/exportXls',
        importExcelUrl: '/box.ship/shipInfo/importExcel'

      },
      fieldList: superQueryFieldList,
      dictOptions: {},
      tableHeight: 450,
      Footer: '',
      timer: ''
    }
  },
  created() {
    this.loading = true
    this.getRule()
    this.initColumns()
    this.tableHeight = document.documentElement.clientHeight - 350

    let param = { isLock: 0, pageSize: 99999 }
    getAction('/companydata/list', param).then(res => {
      console.log(res.result.records)
      let companyDataList = []
      for (let i = 0; i < res.result.records.length; i++) {
        companyDataList.push({
          value: res.result.records[i].companyName,
          label: res.result.records[i].companyName
        })
      }
      this.fieldList[7].options = companyDataList
      this.fieldList[8].options = companyDataList
    })
    // window.onresize = () => {
    //   console.log(document.documentElement.clientHeight)
    //   return (() => {
    //     this.tableHeight = document.documentElement.clientHeight - 350
    //   })()
    // }
  },
  computed: {
    // importExcelUrl: function(){
    //   return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    // },
  },
  mounted() {
    this.timer = setInterval(() => {
      this.loadDataNoLoading()
    }, 5000)
  },
  destroyed() {
    clearInterval(this.timer)
  },
  methods: {
    refreshData(value, option) {
      console.log(value)
      clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.loadDataNoLoading()
      }, value)
    },
    getRule() {
      let LOGIN_USER_BUTTON_AUTH = sessionStorage.getItem('LOGIN_USER_BUTTON_AUTH')
      let listAuth = JSON.parse(LOGIN_USER_BUTTON_AUTH)
      for (let i = 0; i < listAuth.length; i++) {
        if (listAuth[i].action == 'ship:own') {
          this.queryParam.so = 1
          break
        }
      }
    },
    close() {
      this.loadData()
      this.addModalVisible = false
    },
    handleAdd() {
      this.addModalVisible = true
      this.$nextTick(() => {
        this.$refs.modalForm.addclick()
      })
    },
    handleEdit(record) {
      this.addModalVisible = true
      this.$nextTick(() => {
        this.$refs.modalForm.edit(record)
      })
    },
    handleDetail(record) {
      this.addModalVisible = true
      this.$nextTick(() => {
        this.$refs.modalForm.showdetail(record)
      })
    },
    initDictConfig() {
    },
    handleBoxInfo() {

    },
    //列设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ':colsettings'
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues
      const cols = this.defColumns.filter(item => {
        if (item.key == 'rowIndex' || item.dataIndex == 'action') {
          return true
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      this.columns = cols
    },
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');
      var key = this.$route.name + ':colsettings'
      let colSettings = Vue.ls.get(key)
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = []
        this.defColumns.forEach(function(item, i, array) {
          allSettingColumns.push(item.dataIndex)
        })
        this.settingColumns = allSettingColumns
        this.columns = this.defColumns
      } else {
        this.settingColumns = colSettings
        const cols = this.defColumns.filter(item => {
          if (item.key == 'rowIndex' || item.dataIndex == 'action') {
            return true
          }
          if (colSettings.includes(item.dataIndex)) {
            return true
          }
          return false
        })
        this.columns = cols
      }
    },
    //显示隐藏操作列
    showHiddenActionCol() {
      if (this.actionLabel == '隐藏操作列') {
        this.columns.splice(this.columns.length - 1, 1)
        this.actionLabel = '显示操作列'
      } else {
        this.columns.push(this.actionColumn)
        this.actionLabel = '隐藏操作列'
      }
    },
    portHandleSearch(value) {
      let params = { name: value }
      getAction('/box/dictPort/findbyname', params).then(res => {
        if (res.code == 200) {
          let data = []
          res.result.forEach(item => {
            data.push(item.enName)
          })
          this.portData = data
        }
      })
    },
    handleSelectEdit(record, index) {
      return {
        on: {
          dblclick: () => {
            this.handleEdit(record)
            // console.log(record,index)
          }
        }
      }
    }
  }
}
</script>
<style scoped>
/deep/ .ant-table {
  line-height: 0.1;
}

/deep/ .ant-empty-normal {
  margin: 140px 0 !important;
}

/deep/ .ant-row .ant-form-item {
  margin-bottom: 5px;
}

/deep/ .table-page-search-wrapper .table-page-search-submitButtons {
  margin-bottom: 5px;
}
</style>
<style lang="less">
.resize-table-th {
  position: relative;

  .table-draggable-handle {
    transform: none !important;
    position: absolute;
    height: 100% !important;
    bottom: 0;
    left: auto !important;
    right: -5px;
    cursor: col-resize;
    touch-action: none;
  }
}
</style>
